package com.shuyun.hystrix.dynamic.jdbc;

import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.FixedDelayPollingScheduler;
import com.netflix.config.PolledConfigurationSource;
import com.netflix.config.sources.JDBCConfigurationSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * @description:
 * @author: shuai.chen@shuyun.com
 * @date: 2023/1/17 6:38 PM
 */
@Configuration
public class HystrixPropertyConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public DynamicConfiguration dynamicConfiguration() {
        PolledConfigurationSource source = new JDBCConfigurationSource(dataSource,
                "select distinct prop_key,prop_value from properties", "prop_key", "prop_value");
        return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
    }

    @PostConstruct
    public void init(){
        ConfigurationManager.install(dynamicConfiguration());
    }

}
